<?php

/**
 * 模型对应数据库表 "infosort".
 *
 * 以下是可用表infosort的栏位:
 * @property string $id
 * @property string $sort_name
 * @property string $sort_keyword
 * @property string $sort_describe
 * @property integer $sort_state
 * @property integer $sort_position
 */
class Infosort extends CActiveRecord {

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return Infosort the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'web_infosort';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('sort_name, sort_keyword, sort_describe, sort_state', 'required'),
            array('sort_state, sort_position', 'numerical', 'integerOnly' => true),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, sort_name, sort_keyword, sort_describe, sort_state, sort_position', 'safe', 'on' => 'search'),
        );
    }

    /*
     * 状态是否激活
     */

    public $sort_state_array = array(
        1 => '激活',
        0 => '未激活',
    );

    /**
     * 激活状态标志
     * @param 状态id
     * @author nirsh
     */
    public function getCustomerState($aData) {
        $sUrl = Yii::app()->createUrl('Infosort/setImportant', array(
            'id' => $aData->id,
            'sort_state' => $aData->sort_state,
                ));
        $adsManage_state = $aData->sort_state;
        if ($adsManage_state) {
            $rString = '<a href="' . $sUrl . '" class="important" title="点击取消激活"></a>';
        } else {
            $rString = '<a href="' . $sUrl . '" class="set-important" title="点击激活"></a>';
        }
        return $rString;
    }

    /**
     * 选择判断激活
     * @param type $col 
     */
    public function resetState() {
        $sql = " UPDATE web_infosort SET sort_state= 0 WHERE sort_state=1";
        Yii::app()->db->createCommand($sql)->execute();
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => '系统编号',
            'sort_name' => '分类名称',
            'sort_keyword' => '分类关键字',
            'sort_describe' => '分类描述',
            'sort_state' => '分类状态',
            'sort_position' => '分类序号',
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('sort_name', $this->sort_name, true);
        $criteria->compare('sort_keyword', $this->sort_keyword, true);
        $criteria->compare('sort_describe', $this->sort_describe, true);
        $criteria->compare('sort_state', $this->sort_state);
        $criteria->compare('sort_position', $this->sort_position);

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

}